package com.yanqu.road.task;

import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * 检测死锁定时任务
 */
public class CheckDeadLockTask extends GeneralTask {

    public static Logger deadLockLogger = LogManager.getLogger("deadLock");

    @Override
    public void doRun() {
        try {
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

            long[] deadlockedThreadIds = threadMXBean.findDeadlockedThreads();

            if (deadlockedThreadIds != null) {
                ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(deadlockedThreadIds);
                for (ThreadInfo threadInfo : threadInfos) {
                    deadLockLogger.error("find deadLock,{}", threadInfo);
                }
            }
        } catch (Exception e) {
            deadLockLogger.error("CheckDeadLockTask run error.", e);
        }
    }
}
